package edu.sias.article.controller;

import edu.sias.article.entity.Case;
import edu.sias.article.service.CaseService;
import edu.sias.article.vo.CaseVo;
import edu.sias.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cases")
public class CaseController {

    // 注入CaseService
    @Autowired
    private CaseService caseService;

    /**
     * 插入病例信息
     * @param caseVo 病例实体对象
     * @return 插入成功的记录数
     */
    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody CaseVo caseVo) {
        try {
            int result = caseService.insert(convertToCase(caseVo));
            if (result > 0) {
                return AjaxResult.success("插入成功");
            } else {
                return AjaxResult.error("插入失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("插入失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID删除病例信息
     * @param id 病例ID
     * @return 删除成功的记录数
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteById(@PathVariable String id) {
        try {
            int result = caseService.deleteById(id);
            if (result > 0) {
                return AjaxResult.success("删除成功");
            } else {
                return AjaxResult.error("删除失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 更新病例信息
     * @param caseVo 病例实体对象
     * @return 更新成功的记录数
     */
    @PutMapping("/update")
    public AjaxResult update(@RequestBody CaseVo caseVo) {
        try {
            // 验证输入数据的有效性
            if (caseVo == null || caseVo.getId() == null) {
                return AjaxResult.error("输入数据无效");
            }

            // 从数据库中获取当前的病例信息
            Case existingCase = caseService.selectById(caseVo.getId());
            if (existingCase == null) {
                return AjaxResult.notFound("病例未找到");
            }

            // 将 CaseVo 中的非空值更新到 existingCase 中
            if (caseVo.getDescription() != null) {
                existingCase.setDescription(caseVo.getDescription());
            }
            if (caseVo.getImageUrl() != null) {
                existingCase.setImageUrl(caseVo.getImageUrl());
            }
            if (caseVo.getDiseaseType() != null) {
                existingCase.setDiseaseType(caseVo.getDiseaseType());
            }
            if (caseVo.getDiseaseName() != null) {
                existingCase.setDiseaseName(caseVo.getDiseaseName());
            }
            if (caseVo.getData() != null) {
                existingCase.setData(caseVo.getData());
            }

            // 更新病例信息
            int result = caseService.update(existingCase);
            if (result > 0) {
                return AjaxResult.success("更新成功");
            } else {
                return AjaxResult.error("更新失败");
            }
        } catch (Exception e) {
            // 记录日志
            System.err.println("更新病例信息时发生异常: " + e.getMessage());
            return AjaxResult.error("更新失败: " + e.getMessage());
        }
    }


    /**
     * 根据ID查询病例信息
     * @param id 病例ID
     * @return 病例实体对象
     */
    @GetMapping("/select/{id}")
    public AjaxResult selectById(@PathVariable String id) {
        try {
            Case caseObj = caseService.selectById(id);
            if (caseObj != null) {
                return AjaxResult.success(convertToCaseVo(caseObj));
            } else {
                return AjaxResult.notFound("病例未找到");
            }
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 查询所有病例信息
     * @return 病例信息列表
     */
    @GetMapping("/selectAll")
    public AjaxResult selectAll() {
        try {
            List<Case> cases = caseService.selectAll();
            List<CaseVo> caseVos = cases.stream()
                    .map(this::convertToCaseVo)
                    .collect(Collectors.toList());
            return AjaxResult.success(caseVos);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 将 Case 实体类转换为 CaseVo 视图对象
     * @param aCase 病例实体对象
     * @return 病例视图对象
     */
    private CaseVo convertToCaseVo(Case aCase) {
        CaseVo caseVo = new CaseVo();
        caseVo.setId(aCase.getId());
        caseVo.setDescription(aCase.getDescription());
        caseVo.setImageUrl(aCase.getImageUrl());
        caseVo.setDiseaseType(aCase.getDiseaseType());
        caseVo.setDiseaseName(aCase.getDiseaseName());
        caseVo.setData(aCase.getData());
        return caseVo;
    }

    /**
     * 将 CaseVo 视图对象转换为 Case 实体类
     * @param caseVo 病例视图对象
     * @return 病例实体对象
     */
    private Case convertToCase(CaseVo caseVo) {
        Case aCase = new Case();
        aCase.setId(caseVo.getId());
        aCase.setDescription(caseVo.getDescription());
        aCase.setImageUrl(caseVo.getImageUrl());
        aCase.setDiseaseType(caseVo.getDiseaseType());
        aCase.setDiseaseName(caseVo.getDiseaseName());
        aCase.setData(caseVo.getData());
        return aCase;
    }
}
